<?php

namespace app\model;

use think\Model;

class Role extends Model
{
    protected $name = 'game1';

    protected $pk = 'uid';

    protected $schema = [
        'uid'       => 'int',
        'sid'       => 'string',
        'token'     => 'string',

        'vip'       => 'int',
        'uczb'      => 'int',
        'uyxb'      => 'int',

        'uname'     => 'string',
        'usex'      => 'int',
        'ulv'       => 'int',
        'uexp'      => 'int',

        //四维基础属性
        'utz'       => 'int',   //体质
        'ugg'       => 'int',   //根骨
        'ull'       => 'int',   //力量
        'umj'       => 'int',   //敏捷

        //基础属性
        'uhp'       => 'int',
        'umaxhp'    => 'int',
        'ump'       => 'int',
        'umaxmp'    => 'int',
        'ugj'       => 'int',
        'ufy'       => 'int',
        'ubj'       => 'int',
        'usd'       => 'int',
        'usb'       => 'int',
        'uwx'       => 'int',

        'cw'        => 'int',

        'tool1'     => 'int',
        'tool2'     => 'int',
        'tool3'     => 'int',
        'tool4'     => 'int',
        'tool5'     => 'int',
        'tool6'     => 'int',
        'yp1'       => 'int',
        'yp2'       => 'int',
        'yp3'       => 'int',
        'jn1'       => 'int',
        'jn2'       => 'int',
        'jn3'       => 'int',

        'ispvp'     => 'int',
        'nowmid'    => 'int',
        'nowguaiwu' => 'int',

        'addtime'       => 'int',
        'endtime'       => 'datetime',
        'sfzx'          => 'int',
        'xiuliantime'   => 'datetime',
        'sfxl'          => 'int',
        'qiandaotime'   => 'datetime',

        'isDel'         => 'int',   //是否申请删除，0否，1是
        'delTime'       => 'int',   //申请删除时间
    ];

    /**
     * 获取列表
     * @return array 分页列表
     */
    public function loadList($where = [], $pageAry = [], $order = 'uid desc')
    {
        $data = $this->where($where)->order($order)->paginate($pageAry);
        return $data;
    }

    /**
     * 获取列表
     * @return array 列表
     */
    public function getList($where = array(), $order = 'uid desc')
    {
        $data = $this->where($where)->order($order)->select();
        return $data;
    }

    public function getListLimit($where = array(), $order = 'uid desc', $limit)
    {
        $data = $this->where($where)->limit($limit)->order($order)->select();
        return $data;
    }

    /**
     * 获取信息
     * @param int $ID
     * @return array 信息
     */
    public function getInfo($id)
    {
        $map = array();
        $map['uid'] = $id;
        return $this->getWhereInfo($map);
    }

    /**
     * 获取信息
     * @param array $where 条件
     * @return array 信息
     */
    public function getWhereInfo($where)
    {
        $info = $this->where($where)->find();
        return $info;
    }

    /**
     * 获取字段值
     * @param array $where 条件
     * @param array $field 字段
     * @return field 信息
     */
    public function getWhereValue($where, $field)
    {
        $data = $this->where($where)->value($field);
        return $data;
    }

    /**
     * 获取列的值
     * @param array $where 条件
     * @param array $field 字段
     * @return array 信息
     */
    public function getWhereColumn($where, $field)
    {
        $data = $this->where($where)->column($field);
        return $data;
    }

    /**
     * 新增
     */
    public function addall($request)
    {
        return $this->save($_POST);
    }

    /**
     * 更新
     */
    public function editAll($request)
    {
        return $this->saved($_POST);
    }

    //数据新增
    public function add($data)
    {
        //启动事务
        $this->startTrans();
        try {
            $insertid = self::insertGetId($data);
            //提交事务
            $this->commit();
            return $insertid;
        } catch (\Exception $e) {
            //回滚事务
            $this->rollback();
        }
    }

    //数据更新
    public function saved($data)
    {
        $this->update($data);
        return true;
    }

    /**
     * 删除信息
     * @param int $id ID
     * @return bool 删除状态
     */
    public function del($id)
    {
        $map = array();
        $map['uid'] = $id;
        return $this->where($map)->delete();
    }

    /**
     * 四维属性折算
     * 角色创建或升级 四维属性发生变动后
     * @param $uid
     */
    public function setAttributeConvert($uid)
    {
        $infoRole = $this->getInfo($uid);

        $hp = $infoRole['utz'] * 10;    //生命
        $fy = $infoRole['utz'] * 1;     //防御
        $mp = $infoRole['ugg'] * 10;    //真元
        $wx = $infoRole['ugg'] * 5;     //五行
        $gj = $infoRole['ull'] * 5;     //攻击
        $bj = $infoRole['ull'] * 1;     //暴击
        $sd = $infoRole['umj'] * 1;     //速度
        $sb = $infoRole['umj'] * 1;     //闪避
        
        if($infoRole['cw']!=0){
            $infoRolePets = model('RolePets')->getInfo($infoRole['cw']);
            $hp = $hp + $infoRolePets['uphp'];
            $gj = $gj + $infoRolePets['upgj'];
            $fy = $fy + $infoRolePets['upfy'];
        }
        
        $aryRoleEquip = [$infoRole['tool1'],$infoRole['tool2'],$infoRole['tool3'],$infoRole['tool4'],$infoRole['tool5'],$infoRole['tool6']];
        $aryRoleEquip = array_diff($aryRoleEquip,[0]);
        if (!empty($aryRoleEquip)) {
            $map = [];
            $map[] = ['zbnowid','in',implode(',', $aryRoleEquip)];
            $listRoleEquip = model('RoleEquip')->getList($map);
            if (!empty($listRoleEquip)) {
                foreach ($listRoleEquip as $k=>$item){
                    $hp = $hp + $item['zbhp'];
                    $gj = $gj + $item['zbgj'];
                    $fy = $fy + $item['zbfy'];
                    $bj = $bj + $item['zbbj'];
                    $sd = $sd + $item['zbsd'];
                }
            }
        }

        $temp = [
            'uid'    => $uid,
            'uhp'    => $hp,
            'umaxhp' => $hp,
            'ump'    => $mp,
            'umaxmp' => $mp,
            'ugj'    => $gj,
            'ufy'    => $fy,
            'ubj'    => $bj,
            'usd'    => $sd,
            'usb'    => $sb,
            'uwx'    => $wx,
        ];
        $this->saved($temp);
    }

    public function getRoleAttribute($uid)
    {
        $info = $this->getInfo($uid);

        //吸血只能是装备附带，闪避只能是四维敏捷折算
        $info['uxx'] = 0;

        $aryRoleEquip = [$info['tool1'],$info['tool2'],$info['tool3'],$info['tool4'],$info['tool5'],$info['tool6']];
        $aryRoleEquip = array_diff($aryRoleEquip,[0]);
        if (!empty($aryRoleEquip)) {
            $map = [];
            $map[] = ['zbnowid','in',implode(',', $aryRoleEquip)];
            $listRoleEquip = model('RoleEquip')->getList($map);
            if (!empty($listRoleEquip)) {
                foreach ($listRoleEquip as $k=>$item){
                    $info['uxx'] = $info['uxx'] + $item['zbxx'];
                }
            }
        }

        return $info;
    }

}
